package com.it.you.base.stream;

import com.it.you.pojo.Computer;
import com.it.you.pojo.ItemBasic;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamTest7 {

    public static void main(String[] args) {
        ItemBasic itemBasic1 = new ItemBasic();
        itemBasic1.setMemberPrice(new BigDecimal(9));
        Computer computer1 = new Computer();
        computer1.setId(1L);
        computer1.setIsChargePrice(1);
        computer1.setBuyPrice(new BigDecimal(1));
        computer1.setItemBasic(itemBasic1);

        ItemBasic itemBasic2 = new ItemBasic();
        itemBasic2.setMemberPrice(new BigDecimal(8));
        Computer computer2 = new Computer();
        computer2.setId(2L);
        computer2.setIsChargePrice(0);
        computer2.setBuyPrice(new BigDecimal(2));
        computer2.setItemBasic(itemBasic2);

        ItemBasic itemBasic3 = new ItemBasic();
        itemBasic3.setMemberPrice(new BigDecimal(7));
        Computer computer3 = new Computer();
        computer3.setId(3L);
        computer3.setIsChargePrice(1);
        computer3.setBuyPrice(new BigDecimal(3));
        computer3.setItemBasic(itemBasic3);

        ItemBasic itemBasic4 = new ItemBasic();
        itemBasic4.setMemberPrice(new BigDecimal(6));
        Computer computer4 = new Computer();
        computer4.setId(4L);
        computer4.setIsChargePrice(1);
        computer4.setBuyPrice(new BigDecimal(4));
        computer4.setItemBasic(itemBasic4);

        List<Computer> list = new ArrayList<>();
        list.add(computer1);
        list.add(computer2);
        list.add(computer3);
        list.add(computer4);
        Integer num = 1;
        //使用stream判断集合中是否存在满足条件的数据（只要存在就返回true）
        boolean result = list.stream().anyMatch(co -> num.equals(co.getIsChargePrice()) && co.getBuyPrice() != null && co.getBuyPrice().compareTo(co.getItemBasic().getMemberPrice()) < 0);
        System.out.println(result);

        Map<Long, Computer> map = list.stream().filter(co -> num.equals(co.getIsChargePrice()) && co.getBuyPrice() != null && co.getBuyPrice().compareTo(co.getItemBasic().getMemberPrice()) < 0)
                .collect(Collectors.toMap(Computer::getId, t -> t, (t1, t2) -> t2));
        System.out.println(map);
    }
}
